package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.anno.Log;
import org.example.pojo.Dept;
import org.example.pojo.Result;
import org.example.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /*
    * 查询部门信息
    * */
    @GetMapping
    public Result list(){
        log.info("查询部门信息");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    /*
    * 删除部门数据
    * */
    @Log
    @DeleteMapping
    public Result delete(@RequestParam("id") Integer id){
        log.info("根据ID删除部门数据：{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    /*
    * 新增部门数据
    * */
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门数据：{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    /*
    * 根据ID查询部门
    * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询部门：{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /*
    * 更新部门数据
    * */
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("更新部门数据：{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
